package com.gem.growingtree.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.gem.growingtree.model.PageBean4;
import com.gem.growingtree.model.StudentsAccount;
import com.gem.growingtree.service.StudentsAccountService;

@Controller
public class StudentsAccountController {
	@RequestMapping("/studentsAccount")
	public ModelAndView ShowStudentsAccount(HttpServletRequest request, HttpServletResponse response){
		int pageCode = 0;
		String pc = request.getParameter("pc");
		String  classId = request.getParameter("classId");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		StudentsAccountService sas = new StudentsAccountService();
		try {
			PageBean4 pb = sas.findByPage(pageCode, pageSize, classId);
			request.setAttribute("pageBean4", pb);
			return new ModelAndView("/class");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	// 查找某个学生信息
	@RequestMapping("/showCertainStudent")
	public ModelAndView ShowStudentsAccount2(HttpServletRequest request, HttpServletResponse response,String showS){
		int pageCode = 0;
		String pc = request.getParameter("pc");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		StudentsAccountService sas = new StudentsAccountService();
		try {
			PageBean4 pb = sas.findByPage2(pageCode, pageSize,showS);
			request.setAttribute("pageBean4", pb);
			return new ModelAndView("/class");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	// 查找某个班级全部学生信息
	@RequestMapping("/showDetailStudent")
	public ModelAndView ShowStudentsAccount3(HttpServletRequest request, HttpServletResponse response,String classId){
		int pageCode = 0;
		String pc = request.getParameter("pc");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		StudentsAccountService sas = new StudentsAccountService();
		try {
			PageBean4 pb = sas.findByPage2(pageCode, pageSize,classId);
			request.setAttribute("pageBean4", pb);
			return new ModelAndView("/class");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	// 修改某个学生信息
	@RequestMapping("/modifyStudent")
	public String modifyClasses(HttpServletRequest request,
			HttpServletResponse response, StudentsAccount studentsAccount) {
		String babyId = request.getParameter("babyId");
		if (babyId != null) {
			try {
				StudentsAccountService sas = new StudentsAccountService();
				sas.updateStudent(babyId,studentsAccount);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "/modifyStudent";
	}
	//删除某个学生的信息
	@RequestMapping("/deleteStudent")
	public ModelAndView deleteClassesInfo( HttpServletRequest request,
			HttpServletResponse response) throws Exception {	

		String  babyId = request.getParameter("babyId");
		String classId = request.getParameter("classId");
		StudentsAccountService sas = new StudentsAccountService();
		sas.deleteStudentInfo(babyId);
		int current = 0;
		PageBean4  pageBean4 = sas.findByPage(current, 4,classId);
		request.setAttribute("pageBean", pageBean4);
		return new ModelAndView("/class", "pageBean", pageBean4);
	}
}
